//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.mass.core.activiti.workflow.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.beanutils.Converter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.log4j.Logger;

public class DateConverter implements Converter {
    private static final Logger logger = Logger.getLogger(DateConverter.class);
    @SuppressWarnings("unused")
	private static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    @SuppressWarnings("unused")
	private static final String DATETIME_PATTERN_NO_SECOND = "yyyy-MM-dd HH:mm";
    @SuppressWarnings("unused")
	private static final String DATE_PATTERN = "yyyy-MM-dd";
    @SuppressWarnings("unused")
	private static final String MONTH_PATTERN = "yyyy-MM";

    public DateConverter() {
    }

    public Object convert(@SuppressWarnings("rawtypes") Class type, Object value) {
        Object result = null;
        if(type == Date.class) {
            try {
                result = this.doConvertToDate(value);
            } catch (ParseException var5) {
                var5.printStackTrace();
            }
        } else if(type == String.class) {
            result = this.doConvertToString(value);
        }

        return result;
    }

    private Date doConvertToDate(Object value) throws ParseException {
        Date result = null;
        if(value instanceof String) {
            result = DateUtils.parseDate((String)value, new String[]{"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM"});
            if(result == null && StringUtils.isNotEmpty((String)value)) {
                try {
                    result = new Date((new Long((String)value)).longValue());
                } catch (Exception var4) {
                    logger.error("Converting from milliseconds to Date fails!");
                    var4.printStackTrace();
                }
            }
        } else if(value instanceof Object[]) {
            Object[] array = (Object[])value;
            if(array.length >= 1) {
                value = array[0];
                result = this.doConvertToDate(value);
            }
        } else if(Date.class.isAssignableFrom(value.getClass())) {
            result = (Date)value;
        }

        return result;
    }

    private String doConvertToString(Object value) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String result = null;
        if(value instanceof Date) {
            result = simpleDateFormat.format(value);
        }

        return result;
    }
}
